//------------------------------------------------------------------------------
#include <QtGui>
#include <QMessageBox>
#include <QDate>
#include <QSqlRecord>
#include "CFindFrm.h"
#include "CNotesFrm.h"
#include "CMySQL.h"
//------------------------------------------------------------------------------
CNotesFrm::CNotesFrm(QString name, QString lastCode, QWidget *parent) : CMyMainWindow(name, parent)
{
	setActionCur(CGlobal::acConstruct);
	
	setupUi(this);
	
	setStatusBar(NULL);
	
	masterTable="notes";
	primaryKey="code";
	
	loadSchema();
	
	if(CMySQL::getInstance()->getNbEnregistrement(masterTable) == 0)
	{
		setActionCur(CGlobal::acTableVide);
	}else
	{
		if(!chargeDonnees(lastCode, CGlobal::seNone)) chargeDonnees("", CGlobal::sePremier);
	}
}
//------------------------------------------------------------------------------
void CNotesFrm::setActionCur(CGlobal::EAction action)
{
	actionCur=action;
	
	switch(actionCur)
	{
		case CGlobal::acNone:
			actPremiereFiche->setEnabled(true);
			actFichePrecedente->setEnabled(true);
			actFicheSuivante->setEnabled(true);
			actDerniereFiche->setEnabled(true);
			
			actAjouter->setEnabled(true);
			actSupprimer->setEnabled(true);
			
			actValider->setEnabled(false);
			actAnnuler->setEnabled(false);
			
			actRechercheUneFiche->setEnabled(true);
			
			enableChamps(true);
			code->setEnabled(false);
			break;
		case CGlobal::acTableVide:
			actPremiereFiche->setEnabled(false);
			actFichePrecedente->setEnabled(false);
			actFicheSuivante->setEnabled(false);
			actDerniereFiche->setEnabled(false);
			
			actAjouter->setEnabled(true);
			actSupprimer->setEnabled(false);
			
			actValider->setEnabled(false);
			actAnnuler->setEnabled(false);
			
			actRechercheUneFiche->setEnabled(false);
			
			clearChamps();
			enableChamps(false);
			creation->setValue("");
			break;
		case CGlobal::acAjout:
			clearChamps();
			initChamps();
			enableChamps(true);
			code->setFocus();
		case CGlobal::acModif:
			actPremiereFiche->setEnabled(false);
			actFichePrecedente->setEnabled(false);
			actFicheSuivante->setEnabled(false);
			actDerniereFiche->setEnabled(false);
			
			actAjouter->setEnabled(false);
			actSupprimer->setEnabled(false);
			
			actValider->setEnabled(true);
			actAnnuler->setEnabled(true);
			
			actRechercheUneFiche->setEnabled(false);
		default:
			break;
	}
}
//------------------------------------------------------------------------------
CGlobal::EAction CNotesFrm::getActionCur(void)
{
	return actionCur;
}
//------------------------------------------------------------------------------
void CNotesFrm::closeEvent(QCloseEvent * event)
{
	if(getActionCur() != CGlobal::acNone && getActionCur() != CGlobal::acTableVide)
	{
		QMessageBox msgBox(	QMessageBox::Warning, tr("Attention"), 
							tr("Veuillez annuler ou valider avant de sortir"), QMessageBox::Ok);
		msgBox.exec();	
		
		event->ignore();
	}else
	{
		emit quit(objectName(), curCode);
		event->accept();
	}
}
//------------------------------------------------------------------------------
void CNotesFrm::showFormulaire(QSqlQuery *query, CGlobal::EAction& action)
{
	QSqlRecord rec=query->record();
	
	curCode=rec.value("code").toString();
	code->setValue(curCode);
	nom->setValue(rec.value("nom"));
	creation->setValue(rec.value("creation"));
}
//------------------------------------------------------------------------------
void CNotesFrm::on_actAjouter_triggered(void)
{
	setActionCur(CGlobal::acAjout);
	
	creation->setValue(CMySQL::getInstance()->getCurrentDate());
}
//------------------------------------------------------------------------------
void CNotesFrm::on_actValider_triggered(void)
{
	QString requete;
	QString condition;
	
	code->setText(code->text().toUpper());
	calculChamps(getActionCur());
	
	if(!validChamps()) return;
	if(!validLiaison()) return;
	
	switch(getActionCur())
	{
		case CGlobal::acAjout:
			requete="INSERT INTO "+masterTable+" SET "+primaryKey+"=\""+code->getValue()+"\", ";
			requete+="creation=CURDATE(), createur=\""+CMySQL::getInstance()->getUser()+"\", ";
			condition="";
			break;
		case CGlobal::acModif:
			requete="UPDATE "+masterTable+" SET ";
			requete+="modification=CURDATE(), modificateur=\""+CMySQL::getInstance()->getUser()+"\", ";
			condition="WHERE "+primaryKey+"=\""+code->getValue()+"\"";
			break;
		default:
			return;
	}
	
	requete+="nom = \""+nom->getValue()+"\" ";
	requete+=condition;
	
	if(CMySQL::getInstance()->execQuery(requete) != 0)
	{
		curCode=code->text();
		setActionCur(CGlobal::acNone);
	}
}
//------------------------------------------------------------------------------
void CNotesFrm::on_actAnnuler_triggered(void)
{
	
	QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
						tr("Etes vous sûre de vouloir annuler ?"), QMessageBox::Yes | QMessageBox::No);
	msgBox.exec();
	
	if(msgBox.result() == QMessageBox::Yes)
	{
		if(!chargeDonnees(curCode, CGlobal::seNone))
		{
			if(!chargeDonnees(curCode, CGlobal::sePrecedent))
			{
				if(!chargeDonnees(curCode, CGlobal::seSuivant))
				{
					setActionCur(CGlobal::acTableVide);
					return;
				}
			}
		}
		
		setActionCur(CGlobal::acNone);
	}
}
//------------------------------------------------------------------------------
void CNotesFrm::on_actSupprimer_triggered(void)
{
	QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
						tr("Etes vous sûre de vouloir supprimer la fiche courante ?"), QMessageBox::Yes | QMessageBox::No);
	msgBox.exec();
	
	if(msgBox.result() == QMessageBox::Yes)
	{
		if(!CMySQL::getInstance()->canDelete("article", "unite", curCode))
			return;
		if(!CMySQL::getInstance()->canDelete("ligneDevis", "unite", curCode))
			return;
		if(!CMySQL::getInstance()->canDelete("ligneFactures", "unite", curCode))
			return;
		
		QString requete="DELETE FROM "+masterTable+" WHERE "+primaryKey+"=\""+curCode+"\"";
		
		CMySQL::getInstance()->execQuery(requete);
		
		if(!chargeDonnees(curCode, CGlobal::sePrecedent))
		{
			if(!chargeDonnees(curCode, CGlobal::seSuivant))
			{
				setActionCur(CGlobal::acTableVide);
			}
		}
	}
}
//------------------------------------------------------------------------------
void CNotesFrm::onChange(void)
{
	if(getActionCur() == CGlobal::acNone)
	{
		setActionCur(CGlobal::acModif);
	}
}
//------------------------------------------------------------------------------
void CNotesFrm::on_actRechercheUneFiche_triggered(void)
{
	CFindFrm *findFrm=new CFindFrm(masterTable, this);
	
	findFrm->setModal(true);
	findFrm->exec();
	if(findFrm->result() == QDialog::Accepted)
	{
		chargeDonnees(findFrm->getResult(), CGlobal::seNone);
	}
}
//------------------------------------------------------------------------------
void CNotesFrm::onInvokeHelp(CChamp *champ)
{
	QMessageBox msgBox(	QMessageBox::Information, champ->getDescription(), 
						QObject::tr("Dans la base: ")+champ->getTable()+"."+champ->getChamp(), QMessageBox::Ok);
	msgBox.exec();	
}
//------------------------------------------------------------------------------
